package routers

import (
	"io"
	"os"
	adminAuth "server/controller/auth"
	"server/controller/demo"
	adminDownload "server/controller/download"
	adminRole "server/controller/role"
	adminUser "server/controller/user"
	wechat "server/controller/wechat"
	"server/middleware"
	"server/setting"

	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"

	_ "server/docs"
)

func SetupRouter() *gin.Engine {
	if setting.Conf.Release {
		gin.SetMode(gin.ReleaseMode)
	}
	// 写入日志文件
	f, _ := os.Create("logs/gin.log")
	// 将日志写入文件和控制台
	gin.DefaultWriter = io.MultiWriter(f, os.Stdout)

	r := gin.Default()

	// url := ginSwagger.URL("http://localhost:7071/swagger/doc.json") // The url pointing to API definition
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	// r.GET("/swagger/*any", ginSwagger.DisablingWrapHandler(swaggerFiles.Handler, "NAME_OF_ENV_VARIABLE"))

	// r := gin.New()
	// r.Use(gin.Logger())
	r.NoMethod(middleware.HandleNotFound)
	r.NoRoute(middleware.HandleNotFound)
	r.Use(middleware.ErrHandler())
	r.Use(middleware.JwtAuthHandler())
	// api
	apiGroup := r.Group("/api")
	{
		adminGroup := apiGroup.Group("/admin")
		{
			// AdminUser
			adminGroup.POST("/user", adminUser.Create)
			adminGroup.GET("/user", adminUser.Get)
			adminGroup.PUT("/user/:id", adminUser.Update)
			adminGroup.DELETE("/user/:id", adminUser.Delete)
			adminGroup.POST("/login", adminUser.Login)
			adminGroup.GET("/userinfo", adminUser.UserInfo)
			adminGroup.POST("/change_password", adminUser.ChangePassword)
			adminGroup.POST("/verifi_code", adminUser.GetVerifiCode)

			// AdminRole
			adminGroup.POST("/role", adminRole.Create)
			adminGroup.GET("/role", adminRole.Get)
			adminGroup.PUT("/role/:id", adminRole.Update)
			adminGroup.DELETE("/role/:id", adminRole.Delete)
			// AdminAuth
			adminGroup.POST("/auth", adminAuth.Create)
			adminGroup.GET("/auth", adminAuth.Get)
			adminGroup.PUT("/auth/:id", adminAuth.Update)
			adminGroup.DELETE("/auth/:id", adminAuth.Delete)

			// AdminDownload
			adminGroup.GET("/download", adminDownload.Get)
			adminGroup.POST("/download", adminDownload.Create)
		}

		demoGroup := apiGroup.Group("/demo")
		{
			demoGroup.POST("/voice", demo.PostTextToVoice)
			demoGroup.POST("/email", demo.SendEmail)
			demoGroup.GET("/excel/export", demo.ExcelExport)
			demoGroup.POST("/excel/import", demo.ExcelImport)
			demoGroup.GET("/excel/zip_export", demo.ZipExport)
			demoGroup.POST("/cos/put", demo.CosPutHandle)
			demoGroup.GET("/wx/getuserinfo", wechat.GetUserInfo)
			demoGroup.GET("/wx/sendtmpmsg", wechat.SendTmpMsg)
			demoGroup.GET("/mq/rdbpush", demo.RdbListPushHandle)
			demoGroup.GET("/mq/goroutine", demo.GoRoutineHandle)
		}
	}

	return r
}
